package com.wxprogram.smallprogram.service.impl;

import com.wxprogram.smallprogram.dao.IFindstudent;
import com.wxprogram.smallprogram.dao.IFindteacher;
import com.wxprogram.smallprogram.dao.SaveDao;
import com.wxprogram.smallprogram.entity.FindStudent;
import com.wxprogram.smallprogram.entity.FindTeacher;
import com.wxprogram.smallprogram.entity.StudentSave;
import com.wxprogram.smallprogram.entity.TeacherSave;
import com.wxprogram.smallprogram.entity.utilEntity.PageUtil;
import com.wxprogram.smallprogram.service.SaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户收藏业务逻辑
 */
@Service
public class SaveServiceImpl implements SaveService {
    @Autowired
    private SaveDao saveDao;
    @Autowired
    private IFindteacher iFindteacher;
    @Autowired
    private IFindstudent iFindstudent;

    /**
     * 学生收藏
     */

    /**
     * 查询学生是否已收藏该发布
     * @param openID
     * @param code
     * @return
     */
    @Override
    public int selectStudentIsSave(String openID,int code) {
        StudentSave studentSave = new StudentSave();
        studentSave.setOpenID(openID);
        studentSave.setCode(code);
        return saveDao.selectStudentIsSave(studentSave);
    }

    /**
     * 根据收藏状态收藏或者取消收藏
     * @param openID
     * @param code
     * @param status
     * @return
     */

    @Override
    public int studentSave(String openID, int code, int status) {
        if (status == 0){
            iFindstudent.updateSaveNum(1,code);
            return insertStudentSave(openID,code);
        }else {
            iFindstudent.updateSaveNum(-1,code);
            return deleteStudentSave(openID,code);
        }
    }

    /**
     * 获取学生的收藏
     * @param openID
     * @param current
     * @return
     */
    @Override
    public PageUtil<FindStudent> getStudentSaveList(String openID, int current) {
        PageUtil<FindStudent> page = new PageUtil<>(current);
        List<FindStudent> list = iFindstudent.selectSaveList(openID,page.getOffset(),page.getSize());
        int count = iFindstudent.getSaveCount(openID);
        page.setList(list);
        page.setCount(count);

        return page;
    }

    /**
     *  学生收藏该发布
     * @param openID
     * @param code
     * @return
     */
    private int insertStudentSave(String openID,int code) {
        StudentSave studentSave = new StudentSave();
        studentSave.setOpenID(openID);
        studentSave.setCode(code);
        return saveDao.insertStudentSave(studentSave);
    }

    /**
     * 学生取消收藏
     * @param openID
     * @param code
     * @return
     */
    private int deleteStudentSave(String openID,int code) {
        StudentSave studentSave = new StudentSave();
        studentSave.setOpenID(openID);
        studentSave.setCode(code);
        return saveDao.deleteStudentSave(studentSave);
    }

    /**
     * 研究生收藏
     */

    /**
     * 查询研究生是否已收藏该发布
     * @param openID
     * @param code
     * @return
     */
    @Override
    public int selectTeacherIsSave(String openID,int code) {
        TeacherSave teacherSave = new TeacherSave();
        teacherSave.setOpenID(openID);
        teacherSave.setCode(code);
        return saveDao.selectTeacherIsSave(teacherSave);
    }

    /**
     * 根据收藏状态收藏或者取消收藏
     * @param openID
     * @param code
     * @param status
     * @return
     */
    @Override
    public int teacherSave(String openID, int code, int status) {
        if (status == 0){
            iFindteacher.updateSaveNum(1,code);
            return insertTeacherSave(openID,code);
        }else {
            iFindteacher.updateSaveNum(-1,code);
            return deleteTeacherSave(openID,code);
        }
    }

    /**
     * 获取研究生的收藏
     * @param openID
     * @param current
     * @return
     */
    @Override
    public PageUtil<FindTeacher> getTeacherSaveList(String openID, int current) {
        PageUtil<FindTeacher> page = new PageUtil<>(current);
        List<FindTeacher> list = iFindteacher.selectSaveList(openID,page.getOffset(),page.getSize());
        int count = iFindteacher.getSaveCount(openID);
        page.setCount(count);
        page.setList(list);
        return page;
    }

    /**
     * 研究生收藏该发布
     * @param openID
     * @param code
     * @return
     */
    private int insertTeacherSave(String openID,int code) {
        TeacherSave teacherSave = new TeacherSave();
        teacherSave.setOpenID(openID);
        teacherSave.setCode(code);
        return saveDao.insertTeacherSave(teacherSave);
    }

    /**
     * 研究生取消收藏
     * @param openID
     * @param code
     * @return
     */
    private int deleteTeacherSave(String openID,int code) {
        TeacherSave teacherSave = new TeacherSave();
        teacherSave.setOpenID(openID);
        teacherSave.setCode(code);
        return saveDao.deleteTeacherSave(teacherSave);
    }

}
